/**
 * 距离计算功能测试
 */

const locationUtil = require('./utils/location.js');

// 测试数据
const testCases = [
  {
    name: '北京天安门到故宫',
    lat1: 39.9042,
    lng1: 116.4074,
    lat2: 39.9163,
    lng2: 116.3972,
    expectedDistance: 1500 // 大约1.5公里
  },
  {
    name: '上海外滩到东方明珠',
    lat1: 31.2304,
    lng1: 121.4737,
    lat2: 31.2397,
    lng2: 121.4998,
    expectedDistance: 2500 // 大约2.5公里
  },
  {
    name: '广州塔到珠江',
    lat1: 23.1066,
    lng1: 113.3213,
    lat2: 23.1066,
    lng2: 113.3313,
    expectedDistance: 800 // 大约800米
  }
];

// 测试距离计算
console.log('=== 距离计算测试 ===');
testCases.forEach(testCase => {
  const distance = locationUtil.calculateDistance(
    testCase.lat1, 
    testCase.lng1, 
    testCase.lat2, 
    testCase.lng2
  );
  const formattedDistance = locationUtil.formatDistance(distance);
  
  console.log(`${testCase.name}:`);
  console.log(`  计算距离: ${distance.toFixed(0)}m`);
  console.log(`  格式化距离: ${formattedDistance}`);
  console.log(`  预期距离: ${testCase.expectedDistance}m`);
  console.log(`  误差: ${Math.abs(distance - testCase.expectedDistance).toFixed(0)}m`);
  console.log('');
});

// 测试坐标解析
console.log('=== 坐标解析测试 ===');
const posStrings = [
  '39.9042,116.4074',
  '31.2304,121.4737',
  'invalid',
  '39.9042',
  ''
];

posStrings.forEach(posStr => {
  const result = locationUtil.parsePosition(posStr);
  console.log(`解析 "${posStr}":`, result);
});

// 测试商家列表距离计算
console.log('=== 商家列表距离计算测试 ===');
const mockBizList = [
  {
    id: 1,
    name: '商家A',
    pos: '39.9042,116.4074'
  },
  {
    id: 2,
    name: '商家B',
    pos: '39.9163,116.3972'
  },
  {
    id: 3,
    name: '商家C',
    pos: 'invalid'
  },
  {
    id: 4,
    name: '商家D',
    pos: null
  }
];

const userLocation = {
  latitude: 39.9102,
  longitude: 116.4024
};

const bizListWithDistance = locationUtil.addDistanceToBizList(mockBizList, userLocation);
console.log('添加距离后的商家列表:');
bizListWithDistance.forEach(biz => {
  console.log(`${biz.name}: ${biz.distanceText} (${biz.distance ? biz.distance.toFixed(0) + 'm' : '未知'})`);
});

// 测试距离排序
console.log('\n=== 距离排序测试 ===');
const sortedBizList = locationUtil.sortBizListByDistance(bizListWithDistance);
console.log('按距离排序后的商家列表:');
sortedBizList.forEach(biz => {
  console.log(`${biz.name}: ${biz.distanceText} (${biz.distance ? biz.distance.toFixed(0) + 'm' : '未知'})`);
}); 